package com.aegean.MonitorExamSYS.center.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.aegean.MonitorExamSYS.center.business.CenterBusiness;
import com.aegean.MonitorExamSYS.center.component.SelectPosition;
import com.aegean.MonitorExamSYS.tools.MyTools;
import com.aegean.MonitorExaxSYS.model.MonitorValue;
import com.opensymphony.xwork2.ActionSupport;

@Controller("AutoArrangeForMonitor")
@Scope("prototype")
public class AutoArrangeForMonitor extends ActionSupport implements
		ServletRequestAware {
	@Resource
	CenterBusiness business;
	private HttpServletRequest request;
	private HttpSession session;
	private String msg;
	private boolean success=true;
	private Map<Integer, List<Integer>> monitorSelectedMap;// 定义每个考场的监考人员信息
	private List<SelectPosition> positions;
	private int exam_Id;
	private Date startTime;
	private Date endTime;
	private int big_Id;

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public int getExam_Id() {
		return exam_Id;
	}

	public void setExam_Id(int exam_Id) {
		this.exam_Id = exam_Id;
	}

	public int getBig_Id() {
		return big_Id;
	}

	public void setBig_Id(int big_Id) {
		this.big_Id = big_Id;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		monitorSelectedMap = new HashMap<Integer, List<Integer>>();
		positions = (List<SelectPosition>) session
				.getAttribute("SelectPositionForMontior");
		big_Id = business.searchBigId(exam_Id);
		for (SelectPosition position : positions) {
			List<MonitorValue> monitors = new ArrayList<MonitorValue>();
			monitors.addAll(business.showMonitorsListForMonitor(
					position.getUnit_Id(), startTime, endTime));
			//System.out.println(monitors.size());
			if (monitors.size() < 2) {
				success = false;
				msg = "对不起," + position.getUnit_Name() + "考点的"
						+ position.getName() + "考场安排出错，原因在于该考点的监考人数在"
						+ MyTools.toDateString2(startTime) + "和"
						+ MyTools.toDateString2(endTime) + "之间不足，请联系考点当地增加监考人员！";
				break;
			} else {
				monitors = sortAutoArrange(monitors);
				List<Integer> monitor_Ids = new ArrayList<Integer>();
				monitor_Ids.add(monitors.get(0).getUser_Id());
				monitor_Ids.add(monitors.get(1).getUser_Id());
				monitorSelectedMap.put(position.getId(), monitor_Ids);
			}
		}
		if (success == false) {

		} else {
			success = true;
			msg = "恭喜,自动安排监考人员成功！";
			Set<Integer> po_Ids=monitorSelectedMap.keySet();
			for (Integer i : po_Ids) {
				List<Integer> mo_Ids=monitorSelectedMap.get(i);
				for (Integer integer : mo_Ids) {
					System.out.println("---"+integer);
				}
			}
			session.setAttribute("MonitorSelectedMap", monitorSelectedMap);
		}
		return SUCCESS;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		this.request = arg0;
		this.session = request.getSession();

	}

	public List<MonitorValue> sortAutoArrange(List<MonitorValue> monitors)
			throws Exception {
		List<MonitorValue> needList = new ArrayList<MonitorValue>();
		List<MonitorValue> prepareList = new ArrayList<MonitorValue>();
		for (MonitorValue one : monitors) {
			if (one.getMonitor_Big().getBig_Id() == big_Id) {
				needList.add(one);
			} else {
				prepareList.add(one);
			}
		}
		if (needList.size() >= 2) {
			return needList.subList(0, 2);
		} else if (needList.size() > 0) {
			List<MonitorValue> list = new ArrayList<MonitorValue>();
			list.add(needList.get(0));
			list.add(prepareList.get((int) Math.random() * prepareList.size()));
			return list;
		} else {
			return prepareList.subList(0, 2);
		}
	}
}
